package worldinruin.gui.imp.gameview;
import java.awt.GridLayout;

import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import worldinruin.GameDataHandler;
import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.ISoldier;
import worldinruin.core.ITroop;
import worldinruin.resources.Components;
import worldinruin.resources.Keys;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TroopPanel extends javax.swing.JPanel {
	private JButton divideArmyButton;
	private JPanel unitsPanel;
	private Action listener;
	private ITroop troop;
	
	/**
	 * Creates a troop panel.
	 * @param troop Troop.
	 */
	public TroopPanel(ITroop troop) {
		super();
		this.troop = troop;
		initGUI();
		updateGUI();
		initListener();
	}

	/**
	 * Initializes listeners to components.
	 */
	private void initListener() {
		listener = ControlFactory.getControl(Controllers.GAMEBOARD_CONTROLLER);
		divideArmyButton.addActionListener(listener);
		listener.putValue(Components.divideTroopViewButton, divideArmyButton);
		listener.putValue(Keys.DIVIDETROOPVIEW.toString(), troop);
		
	}

	/**
	 * Initializes the GUI.
	 */
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(200, 400));
			GroupLayout thisLayout = new GroupLayout((JComponent)this);
			this.setLayout(thisLayout);
			{
				divideArmyButton = new JButton();
				divideArmyButton.setText("Divide army");
				divideArmyButton.setLayout(null);
			}
			{
				unitsPanel = new JPanel();
				GridLayout unitPanelLayout = new GridLayout(troop.getSoldiers().size(), 1);
				unitPanelLayout.setColumns(1);
				unitPanelLayout.setHgap(5);
				unitPanelLayout.setVgap(5);
				unitsPanel.setLayout(unitPanelLayout);
			}
				thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(unitsPanel, 0, 316, Short.MAX_VALUE)
					.addGap(26)
					.addComponent(divideArmyButton, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
					.addContainerGap());
				thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(thisLayout.createParallelGroup()
					    .addComponent(unitsPanel, GroupLayout.Alignment.LEADING, 0, 176, Short.MAX_VALUE)
					    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
					        .addGap(0, 21, Short.MAX_VALUE)
					        .addComponent(divideArmyButton, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
					        .addGap(35)))
					.addContainerGap());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Updates the GUI components.
	 */
	public void updateGUI() {
		divideArmyButton.setVisible(troop.getPlayer() == GameDataHandler.getInstance().getGame().getActivePlayer());
		for (ISoldier s : troop.getSoldiers()) {
			JPanel unitPanel = new UnitInformationPanel(s, troop.getAmount(s));
			unitsPanel.add(unitPanel);
		}
	}
}